package discrete.math;

import java.awt.Component;
import java.util.ArrayList;

/*
 * PathPanel.java
 *
 * Created on April 30, 2008, 9:06 AM
 */
import javax.swing.JOptionPane;

/**
 *
 * @author  bryanfetterolf
 */
public class PathPanel extends javax.swing.JPanel {

    private MatrixMath m;
    private Paths path;
    private char start;
    private char end;
    private int pathLength;
    private int numVertices;

    /** Creates new form PathPanel */
    public PathPanel(MatrixMath matrix) {
        m = matrix;
        initComponents();
    }

    PathPanel(boolean doubleBuffered, MatrixMath matrix) {
        this(matrix);
        this.setDoubleBuffered(doubleBuffered);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        startTextField = new javax.swing.JTextField();
        endTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        pathButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        pathTextField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        jLabel1.setText("Start:");

        startTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("End:");

        pathButton.setText("Show Paths");
        pathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Path Length:");

        pathTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathTextFieldActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jTextPane1);

        jScrollPane2.setViewportView(jScrollPane1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(layout.createSequentialGroup()
                .add(116, 116, 116)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pathButton)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel3)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(startTextField, 0, 0, Short.MAX_VALUE)
                            .add(endTextField, 0, 0, Short.MAX_VALUE)
                            .add(pathTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
        );

        layout.linkSize(new java.awt.Component[] {endTextField, jLabel1, jLabel2, jLabel3, pathTextField, startTextField}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(startTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(endTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(pathTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14)
                .add(pathButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 186, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(70, 70, 70))
        );
    }// </editor-fold>//GEN-END:initComponents

private void pathTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_pathTextFieldActionPerformed

private void pathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathButtonActionPerformed
    jTextPane1.setText("");
    path = new Paths(m);
    Component frame = null;
    try {
        start = startTextField.getText().charAt(0);
        end = endTextField.getText().charAt(0);
        pathLength = Integer.parseInt(pathTextField.getText());
        numVertices = Paths.newMatrix.length;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(frame, "Please enter a number from 1 - 20");
    }


    if (Paths.numberPaths(pathLength, start - 'A', end - 'A') == 0) {
        jTextPane1.setText("There are no paths from " + start + " to " + end +
                " with the length " + pathLength);
    } else {
        jTextPane1.setText("These are the path(s): ");
        ArrayList<String> answer = new ArrayList<String>(path.createPaths(
                start, end, pathLength, numVertices));
        String str = "There are " + answer.size() + " paths with the length " +
                pathLength + " from " + start + " to " + end + "\n\nThe " +
                "calculated number is: " + Paths.numberPaths(pathLength, start - 65, end - 65) +
                "\n\nThese are the Path(s): \n" + path.paths.toString();
        jTextPane1.setText(str);

        // this is only for testing
        for (int j = 0; j < Paths.newMatrix.length; j++) {
            for (int i = 0; i < Paths.newMatrix.length; i++) {
                System.out.print(Paths.newMatrix[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }

}//GEN-LAST:event_pathButtonActionPerformed

private void startTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_startTextFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField endTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JButton pathButton;
    private javax.swing.JTextField pathTextField;
    private javax.swing.JTextField startTextField;
    // End of variables declaration//GEN-END:variables
}
